package tas.co.in.daoimpl.customerinfo;

import java.util.List;
import java.util.StringTokenizer;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import tas.co.in.dao.customerinfo.CustomerInfoConnect;
import tas.co.in.domain.customerinfo.CustomerInfoBean;
import tas.co.in.domain.customerinfo.CustomerRequestBean;

public class CustomerInfoImpl extends SqlMapClientDaoSupport implements
		CustomerInfoConnect {

	TransactionTemplate template = null;

	public void setTemplate(TransactionTemplate template) {
		this.template = template;
	}

	@SuppressWarnings("unchecked")
	public List<CustomerInfoBean> getBufferCustomers() {

		try {
			return getSqlMapClient().queryForList(
					"customerInfo.getbufferCustomers");
		} catch (Exception e) {

			System.out.println(e);
		}
		return null;
	}

	public boolean registerCustomers(final StringTokenizer items,
			final CustomerInfoBean bean2) {

		boolean bool;

		bool = (Boolean) template.execute(new TransactionCallback() {

			public Boolean doInTransaction(TransactionStatus arg0) {

				CustomerInfoBean infoBean = new CustomerInfoBean();
				CustomerInfoBean customerInfoBean = new CustomerInfoBean();
				CustomerInfoBean bean = new CustomerInfoBean();
				CustomerInfoBean infoBean2 = new CustomerInfoBean();

				String customerCode;
				String customerId;
				boolean resultFlag = false;

				Object savePoint = null;

				while (items.hasMoreTokens()) {

					infoBean.setCustomerName(items.nextToken());
					infoBean.setAccountBal(items.nextToken());
					infoBean.setPhoneNumber(items.nextToken());
					infoBean.setDob(items.nextToken());
					infoBean.setGender(items.nextToken());
					infoBean.setGeoLatitude(items.nextToken());
					infoBean.setGeoLongitude(items.nextToken());
					infoBean.setAddress(items.nextToken());
					infoBean.setCity(items.nextToken());
					infoBean.setState(items.nextToken());
					infoBean.setArea(items.nextToken());
					infoBean.setCountry(items.nextToken());
					infoBean.setZipcode(items.nextToken());
					infoBean.setEmailId(items.nextToken());
					infoBean.setImieNumber(items.nextToken());
					infoBean.setUserId(bean2.getUserId());

					try {
						
						System.out.println("1st object");

						customerInfoBean = (CustomerInfoBean) getSqlMapClient()
								.queryForObject("customerInfo.getimages",
										infoBean);

						infoBean.setCustomerImage(customerInfoBean
								.getCustomerImage());
						infoBean.setCustomerPanImage(customerInfoBean
								.getCustomerPanImage());
						infoBean.setVoicePath(customerInfoBean.getVoicePath());
						String code = "FCUSTID";
						savePoint = arg0.createSavepoint();
						infoBean.setCode(code);
						
						System.out.println("2st object");

						bean = (CustomerInfoBean) getSqlMapClient()
								.queryForObject("customerInfo.getsystemvalue",
										infoBean);

						infoBean2.setCode(code);

						if (bean == null) {

							infoBean2.setValue(1);

							getSqlMapClient()
									.insert("customerInfo.insertsystemtable",
											infoBean2);

							customerCode = String.format("%07d", 1);
							break;

						} else {

							customerCode = String.format("%07d", bean
									.getValue());

							infoBean2.setUpdateValue(bean.getValue() + 1);

							getSqlMapClient()
									.update("customerInfo.updatesystemtable",
											infoBean2);

						}

						customerId = "CUST"
								+ customerCode;

						/**
						 * city code to be merged into the account number details
						 */
						infoBean.setAccountNumber(infoBean.getCity()+customerCode);
						infoBean.setCustomerId(customerId);						

						getSqlMapClient().insert(
								"customerInfo.insertcustomers", infoBean);
						getSqlMapClient().update(
								"customerInfo.updatebufferstatus", infoBean);
						
						System.out.println("here after update statement");

						bean = (CustomerInfoBean) getSqlMapClient()
								.queryForObject("customerInfo.getledgernumber");

						infoBean.setLedgerNumber(bean.getLedgerNumber());

						getSqlMapClient().insert(
								"customerInfo.insertledgerdetail", infoBean);
						System.out.println("here after ledger insertion");

						resultFlag = true;

					} catch (Exception e) {
						
						System.out.println(e);

						arg0.rollbackToSavepoint(savePoint);
						
					}
				}

				return resultFlag;
			}

		});

		return bool;
	}

	@SuppressWarnings("unchecked")
	public List<CustomerInfoBean> getRegCustomerList() {
		
		try {
			return getSqlMapClient().queryForList(
					"customerInfo.getregisteredcustomers");
		} catch (Exception e) {

			System.out.println(e);
		}
		return null;
	}
	
		public boolean setBufferCustInfo(CustomerInfoBean bean)
	{
		boolean flag = false;
		try 
		{
			getSqlMapClient().insert("customerInfo.setBufferCustomerInfo", bean);
			flag = true;
		}
		catch (Exception e) 
		{
			System.out.println(e);
		}
		return flag;
	}
		
		 public CustomerInfoBean getCustomers(CustomerInfoBean bean) {
			  // TODO Auto-generated method stub
			  
			  
			  return (CustomerInfoBean) getSqlMapClientTemplate().queryForObject(
			    "customerInfo.getCustomersByPhone", bean);
			 }

			 @SuppressWarnings("unchecked")
			 public List<CustomerRequestBean> getAllRequests(CustomerRequestBean bean) {
			  // TODO Auto-generated method stub
			  
			  return getSqlMapClientTemplate().queryForList("customerInfo.getCustomerRequests", bean);
			 }
			 
				
				
				public List<CustomerInfoConnect> check(CustomerInfoBean bean)
				{
				  try 
				  {
					  System.out.println(bean.getAccountNumber());
					   
					  return getSqlMapClient().queryForList("customerInfo.accselect", bean);
				  }
				  catch (Exception e) 
				  {
					   System.out.println(e);
				  }
				  return null;
				}
				
				public Boolean senddata(CustomerInfoBean bean)
				{
				  Boolean flag = false;
				  try 
				  {	   
					getSqlMapClient().insert("customerInfo.newreqdata",bean);
					flag = true;   
				  }
				  catch (Exception e) 
				  {   
					  System.out.println(e);
			      }
				  return flag;
			   }

					 
					 public List<CustomerInfoBean> proedit()
					 {
					  try
					  {
					   CustomerInfoBean bean = new CustomerInfoBean();
					   bean.setGrpCode("PROEDIT");
					   return getSqlMapClient().queryForList("customerInfo.fetch", bean);
					   
					  } catch (Exception e) 
					  {
					   System.out.println(e);
					  }
					  
					  
					  return null;
					 }
					 
					 public Boolean save(CustomerInfoBean bean) 
					 {
					  Boolean flag = false;
					  try 
					  {
					   
					   getSqlMapClient().insert("customerInfo.save",bean);
					   flag = true;
					   
					  } catch (Exception e)
					  {
					   System.out.println(e);
					  }
					  return flag;
					 }
					 
					 public List<CustomerInfoBean> getData() 
					 {
					  try 
					  {
					   CustomerInfoBean bean = new CustomerInfoBean();
					   bean.setGrpCode("REQTYP");
					   return getSqlMapClient().queryForList("customerInfo.getdetails", bean);
					  }
					  catch (Exception e) 
					  {
					   System.out.println(e);
					  }
					  return null;
					 }
}
